package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.information.domain.back.UserReserveParkBack;
import com.ruoyi.information.mapper.back.UserReserveParkBackMapper;
import com.ruoyi.information.service.back.IUserReserveParkBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 预选产品Controller
 *
 * @author ruoyi
 * @date 2023-05-09
 */
@RestController
@RequestMapping("/parkBack")
public class UserReserveParkBackController extends BaseController {
    @Autowired
    private IUserReserveParkBackService userReserveParkService;

    @Autowired
    private UserReserveParkBackMapper userReserveParkMapper;

    /**
     * 查询预选产品列表
     */
    @RequiresPermissions("information:park:list")
    @GetMapping("/list")
    public TableDataInfo list(UserReserveParkBack userReservePark) {
        startPage();
        List<UserReserveParkBack> list = userReserveParkService.selectUserReserveParkList(userReservePark);
        return getDataTable(list);
    }

    /**
     * 空间类型
     */
    @GetMapping("/space")
    public AjaxResult space() {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "获取成功");
        /*园区*/
        ajaxResult.put("park", userReserveParkMapper.selectParkList());
        /*楼宇商用*/
        ajaxResult.put("buildingCommercial", userReserveParkMapper.selectBuildingCommercialList());
        /*楼宇办公*/
        ajaxResult.put("buildingOffice", userReserveParkMapper.selectBuildingOfficeList());
        /*仓库*/
        ajaxResult.put("warehouse", userReserveParkMapper.selectWarehouseList());
        /*厂房*/
        ajaxResult.put("workShop", userReserveParkMapper.selectWorkShopList());
        /*公寓*/
        ajaxResult.put("apartment", userReserveParkMapper.selectApartmentList());
        /*拍卖*/
        ajaxResult.put("auction", userReserveParkMapper.selectAuctionList());
        /*土地*/
        ajaxResult.put("land", userReserveParkMapper.selectLandList());
        return ajaxResult;
    }

    /**
     * 导出预选产品列表
     */
    @RequiresPermissions("information:park:export")
    @Log(title = "预选产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserReserveParkBack userReservePark) {
        List<UserReserveParkBack> list = userReserveParkService.selectUserReserveParkList(userReservePark);
        ExcelUtil<UserReserveParkBack> util = new ExcelUtil<UserReserveParkBack>(UserReserveParkBack.class);
        util.exportExcel(response, list, "预选产品数据");
    }

    /**
     * 获取预选产品详细信息
     */
    @RequiresPermissions("information:park:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userReserveParkService.selectUserReserveParkById(id));
    }

    /**
     * 新增预选产品
     */
    @RequiresPermissions("information:park:add")
    @Log(title = "预选产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserReserveParkBack userReservePark) {

        UserReserveParkBack back = new UserReserveParkBack();
        back.setSpaceType(userReservePark.getSpaceType());
        back.setPkId(userReservePark.getPkId());
        List<UserReserveParkBack> backs = userReserveParkMapper.selectUserReserveParkList(back);
        if (backs!=null&& !backs.isEmpty()){
            return error("已被预定");
        }

        if (userReservePark.getSpaceType() != null && userReservePark.getPkId() != null) {
            switch (String.valueOf(userReservePark.getSpaceType())) {
                //园区
                case "1": {
                    userReserveParkMapper.updatePark(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
                //商用
                case "2": {
                    userReserveParkMapper.updateBuildingCommercial(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
                //办公
                case "3": {
                    userReserveParkMapper.updateBuildingOffice(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
                //仓库
                case "4": {
                    userReserveParkMapper.updateWarehouse(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
                //厂房
                case "5": {
                    userReserveParkMapper.updateWorkShop(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
                //配套公寓
                case "6": {
                    userReserveParkMapper.updateApartment(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
                //拍卖
                case "7": {
                    userReserveParkMapper.updateAuction(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
                //土地
                case "8": {
                    userReserveParkMapper.updateLand(String.valueOf(userReservePark.getPkId()), "1");
                    break;
                }
            }
        }
        return toAjax(userReserveParkService.insertUserReservePark(userReservePark));
    }

    /**
     * 修改预选产品
     */
    @RequiresPermissions("information:park:edit")
    @Log(title = "预选产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserReserveParkBack userReservePark) {

        UserReserveParkBack userReserveParkBack = userReserveParkService.selectUserReserveParkById(userReservePark.getId());
        if (userReservePark.getReserveState() != null) {
            if (userReserveParkBack != null
                    && userReserveParkBack.getReserveState() != null
                    && !Objects.equals(userReservePark.getReserveState(), userReserveParkBack.getReserveState())) {
                String state = null;
                if (userReservePark.getReserveState() == 1) {
                    state = "1";
                } else {
                    state = "0";
                }
                updatePark(userReserveParkBack, state);
            }
        }
        if (userReservePark.getState()!=null){
            if (userReserveParkBack != null
                    && userReserveParkBack.getState() != null
                    && !Objects.equals(userReservePark.getState(), userReserveParkBack.getState())) {
                String state = null;
                if (userReservePark.getState() == 0) {
                    state = "0";
                } else {
                    state = "5";
                }
                updateParkS(userReserveParkBack,state);
            }
        }

        return toAjax(userReserveParkService.updateUserReservePark(userReservePark));
    }

    /**
     * 删除预选产品
     */
    @RequiresPermissions("information:park:remove")
    @Log(title = "预选产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            UserReserveParkBack userReserveParkBack = userReserveParkService.selectUserReserveParkById(id);
            if (userReserveParkBack != null
                    && userReserveParkBack.getReserveState() != null) {
                String state = "0";
                updatePark(userReserveParkBack, state);

            }
        }
        return toAjax(userReserveParkService.deleteUserReserveParkByIds(ids));
    }

    private void updatePark(UserReserveParkBack userReserveParkBack, String state) {
        if (userReserveParkBack.getSpaceType() != null && userReserveParkBack.getPkId() != null) {
            switch (String.valueOf(userReserveParkBack.getSpaceType())) {
                //园区
                case "1": {
                    userReserveParkMapper.updatePark(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //商用
                case "2": {
                    userReserveParkMapper.updateBuildingCommercial(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //办公
                case "3": {
                    userReserveParkMapper.updateBuildingOffice(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //仓库
                case "4": {
                    userReserveParkMapper.updateWarehouse(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //厂房
                case "5": {
                    userReserveParkMapper.updateWorkShop(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //配套公寓
                case "6": {
                    userReserveParkMapper.updateApartment(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //拍卖
                case "7": {
                    userReserveParkMapper.updateAuction(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //土地
                case "8": {
                    userReserveParkMapper.updateLand(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
            }
        }
    }

    private void updateParkS(UserReserveParkBack userReserveParkBack, String state) {
        if (userReserveParkBack.getSpaceType() != null && userReserveParkBack.getPkId() != null) {
            switch (String.valueOf(userReserveParkBack.getSpaceType())) {
                //园区
                case "1": {
                    userReserveParkMapper.updateParkS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //商用
                case "2": {
                    userReserveParkMapper.updateBuildingCommercialS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //办公
                case "3": {
                    userReserveParkMapper.updateBuildingOfficeS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //仓库
                case "4": {
                    userReserveParkMapper.updateWarehouseS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //厂房
                case "5": {
                    userReserveParkMapper.updateWorkShopS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //配套公寓
                case "6": {
                    userReserveParkMapper.updateApartmentS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //拍卖
                case "7": {
                    userReserveParkMapper.updateAuctionS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
                //土地
                case "8": {
                    userReserveParkMapper.updateLandS(String.valueOf(userReserveParkBack.getPkId()), state);
                    break;
                }
            }
        }
    }
}
